package com.yld.car.market;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yld.car.common.ImageUtils;
import com.yld.car.common.ViewUtils;
import com.yld.car.market.tab.TabActivityGroup;
import com.yld.car.view.PicGallery;

public class GuidePageActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

		setContentView(R.layout.guide_home);

		mPicGallery = (PicGallery) findViewById(R.id.gallery);

		mPicGallery.setAdapter(new PicImageAdapter(all));

		mPicGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapterView, View v,
					int position, long id) {

				selectPosition = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {
				// TODO Auto-generated method stub

			}
		});
	}

	static ArrayList<Integer> all = new ArrayList<Integer>();
	private int selectPosition;
	static {

		all.add(R.drawable.info_0);
		all.add(R.drawable.info_1);
		all.add(R.drawable.info_2);
		all.add(R.drawable.info_3);
		all.add(R.drawable.info_4);
	}
	private PicGallery mPicGallery;

	public void onAction(View v) {
		switch (v.getId()) {
		case R.id.loginButton:

			Intent intent = new Intent();
			intent.setClass(this, LoginActivity.class);
			startActivity(intent);
			finish();
			break;

		case R.id.registerButton:

			Intent intent1 = new Intent();
			intent1.setClass(this, RegisterCompanyActivity.class);
			startActivity(intent1);
			// finish();
			break;
		}

	}

	private class PicImageAdapter extends BaseAdapter {

		private ArrayList<Integer> all;

		public PicImageAdapter(ArrayList<Integer> all) {
			this.all = all;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Integer.MAX_VALUE;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return all.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.gallery_items, null);
			}

			ImageView imageView = (ImageView) convertView
					.findViewById(R.id.pic);

			// Bitmap bitmap = ImageUtils.getBitmapFromResources(
			// GuidePageActivity.this, all.get((position % all.size())));
			// imageView.setImageBitmap(bitmap);
			imageView.setImageResource(all.get((position % all.size())));
			Message msg = myHandler.obtainMessage();
			// msg.arg1 = position;
			// myHandler.sendMessage(msg);
			myHandler.sendEmptyMessage(0);
			return convertView;
		}

	}

	Handler myHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			//
			// System.out.println("selectPostion--" + selectPosition
			// + ",position==" + msg.arg1);
			// System.out.println("handleMessage===>");
			LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.layout_page);
			ViewUtils.addPageView(GuidePageActivity.this, localLinearLayout,
					selectPosition % all.size(), all.size());
		}
	};

	protected void onResume() {

		super.onResume();

		Object o = readObject("userInfo");
		if (o != null) {
			Intent intent = new Intent();
			intent.setClass(this, TabActivityGroup.class);
			startActivity(intent);
			finish();
		}

	}
}
